package com.tgy.MyFlowable.workflow.controller;

import com.ctsi.core.auth.annotation.PreAuth;
import com.ctsi.core.common.api.Result;
import com.ctsi.workflow.dto.FlwModelDTO;
import com.ctsi.workflow.dto.FlwModelInfoDTO;
import com.ctsi.workflow.service.IFlwModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程模型控制器
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 20:20
 */
@RestController
@RequestMapping("/flow/model")
@RequiredArgsConstructor
@Api(tags = "工作流：流程模型")
public class FlwModelController {

    private final IFlwModelService flwModelService;

    /**
     * 模型设计列表
     *
     * @param flwModelDTO 请求参数
     * @return Result 模型列表
     */
    @PreAuth
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public Result<?> list(FlwModelDTO flwModelDTO) {
        return Result.data(flwModelService.list(flwModelDTO));
    }

    /**
     * 模型设计新增
     */
    @PreAuth
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public Result<?> add(@RequestBody FlwModelDTO flwModelDTO) {
        return Result.data(flwModelService.add(flwModelDTO));
    }

    /**
     * 流程模型编辑
     *
     * @param flwModelDTO 模型参数
     * @return Result String
     */
    @PreAuth
    @PostMapping("/edit")
    @ApiOperation(value = "编辑")
    public Result<?> edit(@RequestBody FlwModelDTO flwModelDTO) {
        return Result.data(flwModelService.edit(flwModelDTO));
    }

    /**
     * 流程节点按钮表删除
     *
     * @param ids id字符串，根据,号分隔
     * @return Result
     */
    @PreAuth
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    public Result<?> del(@RequestParam String ids) {
        flwModelService.delete(ids);
        return Result.success("删除成功！");
    }

    /**
     * 模型设计导入
     *
     * @param file 导入文件
     * @return
     */
    @PreAuth
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public Result<?> importModel(@RequestPart("file") MultipartFile file) {
        return Result.data(flwModelService.importModel(file));
    }

    /**
     * 模型设计保存BPMN
     *
     * @param flwModelInfoDTO 保存BPMN
     * @return
     */
    @PreAuth
    @PostMapping("/save-bpmn")
    @ApiOperation(value = "保存BPMN")
    public Result<?> saveBpmnModel(@RequestBody FlwModelInfoDTO flwModelInfoDTO) {
        return Result.data(flwModelService.saveBpmnModel(flwModelInfoDTO));
    }

    /**
     * 模型设计预览
     *
     * @param flwModelDTO
     * @return
     */
    @PreAuth
    @GetMapping("/preview")
    @ApiOperation(value = "预览")
    public Result<?> preview(FlwModelDTO flwModelDTO) {
        return Result.data(flwModelService.preview(flwModelDTO));
    }

    /**
     * 模型设计设为新版
     *
     * @param flwModelDTO
     * @return
     */
    @PreAuth
    @PostMapping("/set-new")
    @ApiOperation(value = "设为新版")
    public Result<?> setNew(@RequestBody FlwModelDTO flwModelDTO) {
        flwModelService.setNew(flwModelDTO);
        return Result.condition(Boolean.TRUE);
    }

    /**
     * 模型设计版本管理
     *
     * @param flwModelDTO
     * @return
     */
    @PreAuth
    @GetMapping("/version")
    @ApiOperation(value = "版本管理")
    public Result<?> version(FlwModelDTO flwModelDTO) {
        return Result.data(flwModelService.version(flwModelDTO));
    }
}

